#ifndef __BUFFER__H_83F24F9C_6264_47B1_8965_909DED272636
#define __BUFFER__H_83F24F9C_6264_47B1_8965_909DED272636

#include "Object.h"

#define DEFAULT_BUFF_SIZE   1024

class CBuff
{
private:
    char* m_pBuff;
    UINT m_nEndPos;
    UINT m_nSize;

public:
    CBuff(IN UINT size = DEFAULT_BUFF_SIZE);
    ~CBuff();

public:
    inline char* GetBuff() const { return m_pBuff; }
    inline UINT GetEndPos() const { return m_nEndPos; }
    inline UINT GetBuffSize() const { return m_nSize; }
    inline BOOL ResetEndPos() { m_nEndPos = 0; return TRUE; }
    inline BOOL AdvanceEndPos(IN UINT len) { if (m_nSize - m_nEndPos >= len) { m_nEndPos += len; return TRUE; } return FALSE; }
    inline BOOL RetreatEndPos(IN UINT len) { if (m_nEndPos >= len ) { m_nEndPos -= len; return TRUE; } return FALSE; }
    inline BOOL Empty() const { return (0 == GetEndPos()); }
    inline BOOL AdvanceMoveBuffMemory(IN UINT StartPos) {
        if (m_nEndPos < StartPos)
            return FALSE;
        else if (m_nEndPos == StartPos)
            return ResetEndPos();
        else
        {
            RetreatEndPos(StartPos);
            memcpy(m_pBuff, m_pBuff + StartPos, m_nEndPos);
            return TRUE;
        }
    }

public:
    BOOL Append(IN const char* data, IN const UINT length);
};


class CSendBuff : public CBuff
{
public:
    CSendBuff(IN UINT size = DEFAULT_BUFF_SIZE);
    ~CSendBuff();

public:
    int Send(IN SOCKET socket);
};


class CRecvBuff : public CBuff
{
public:
    CRecvBuff(IN UINT size = DEFAULT_BUFF_SIZE);
    ~CRecvBuff();

public:
    int Recv(IN SOCKET socket);
};

#endif
